<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Requests\ShipmentCreateRequest;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
use App\Shipment as Shipment;
use App\ShipmentItem as ShipmentItem;
use Illuminate\Support\Facades\DB;
class ShipmentController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $shipments=Shipment::AllOrSearch(Input::get('q'));
        return response()->view("admin.shipments.index",  compact('shipments'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
         return response()->view('admin.shipments.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(ShipmentCreateRequest $request)
    {
        $data = $request->all();
        $all_price=0;
        $all_install_price=0;
        $ship_nums=0;
        foreach($data["goods"] as $k=>$v){
            $ship_nums+=$v["ship_nums"];
            $all_price+=$v["ship_nums"]*$v["ship_price"];
            $all_install_price+=$v["ship_nums"]*$v["ship_install_price"];
            
        }
        $shipment=array();
        $shipment["codeno"]=$data["codeno"];
        $shipment["client_company"]=$data["client_company"];
        $shipment["client_phone"]=$data["client_phone"];
        $shipment["jiaohuo_at"]=$data["jiaohuo_at"];
        $shipment["pay"]=$data["pay"];
        $shipment["ship_nums"]=$ship_nums;
        $shipment["all_price"]=$all_price;
        $shipment["all_install_price"]=$all_install_price;
        if($data["pay"]==($all_price+$all_install_price)){
            $shipment["pay_status"]=2;
        }else if($data["pay"]<($all_price+$all_install_price)&&$data["pay"]>0){
            $shipment["pay_status"]=1;
        }
        if(isset($data["remark"])){
            $shipment["remark"]=$data["remark"];
        }
            
            $model=Shipment::create($shipment);
            foreach($data["goods"] as $k2=>$v2){
                $shipment_item=array();
                $shipment_item["shipment_id"]=$model->id;
                $shipment_item["goods_id"]=$k2;
                $shipment_item["ship_nums"]=$v2["ship_nums"];
                $shipment_item["ship_price"]=$v2["ship_price"];
                $shipment_item["ship_install_price"]=$v2["ship_install_price"];
                $shipment_item["danwei"]=$v2["danwei"];
                ShipmentItem::create($shipment_item);
            }
    
        
        return redirect()->route('admin.shipment.index');
    }
    public function ajaxupdatepay(){
        $shipment_id=Input::get('id');
        $pay=Input::get('pay');
        try{
         $shipment_data=array();
         $shipment_data["pay"]=$pay;
         $shipment_obj=Shipment::findOrFail($shipment_id);
         if($pay==$shipment_obj->all_price){
             $shipment_data["pay_status"]=2; //完全支付
         }else if($pay<$shipment_obj->all_price&&$pay>0){
             $shipment_data["pay_status"]=1; //部分支付
         }else if($pay>$shipment_obj->all_price){
             return response()->json(array('status'=>-2)); //支付超出
         }else{
             $shipment_data["pay_status"]=0; //未支付
         }
         $shipment_obj->update($shipment_data);
         return response()->json(array('status'=>1));
        }catch (ModelNotFoundException $e) {
            return response()->json(array('status'=>-1));
        }
    }
    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        try{
                $shipment=Shipment::findOrFail($id);
                return response()->view('admin.shipments.edit', compact('shipment'));
            }catch (ModelNotFoundException $e) {
                return redirect()->route('admin.nofound');
            }
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(ShipmentCreateRequest $request, $id)
    {
        try{
         $shipment_obj=Shipment::findOrFail($id);
         ShipmentItem::where('shipment_id', $id)->delete();
         $data = $request->all();
         $all_price=0;
         $all_install_price=0;
        $ship_nums=0;
        foreach($data["goods"] as $k=>$v){
            $ship_nums+=$v["ship_nums"];
            $all_price+=$v["ship_nums"]*$v["ship_price"];
            $all_install_price+=$v["ship_nums"]*$v["ship_install_price"];
            
        }
        $shipment=array();
        $shipment["codeno"]=$data["codeno"];
        $shipment["client_company"]=$data["client_company"];
        $shipment["pay"]=$data["pay"];
        $shipment["client_phone"]=$data["client_phone"];
        $shipment["jiaohuo_at"]=$data["jiaohuo_at"];
        $shipment["ship_nums"]=$ship_nums;
        $shipment["all_price"]=$all_price;
        $shipment["all_install_price"]=$all_install_price;
        if(isset($data["remark"])){
            $shipment["remark"]=$data["remark"];
        }
        if($data["pay"]==($all_price+$all_install_price)){
            $shipment["pay_status"]=2;
        }else if($data["pay"]<($all_price+$all_install_price)&&$data["pay"]>0){
            $shipment["pay_status"]=1;
        }
            $shipment_obj->update($shipment);
            foreach($data["goods"] as $k2=>$v2){
                $shipment_item=array();
                $shipment_item["shipment_id"]=$id;
                $shipment_item["goods_id"]=$k2;
                $shipment_item["ship_nums"]=$v2["ship_nums"];
                $shipment_item["ship_price"]=$v2["ship_price"];
                $shipment_item["ship_install_price"]=$v2["ship_install_price"];
                $shipment_item["danwei"]=$v2["danwei"];
                ShipmentItem::create($shipment_item);
            }
            return redirect()->route('admin.shipment.index');
        }catch (ModelNotFoundException $e){
            return redirect()->route('nofound');
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
         try{
            $shipment_obj=Shipment::findOrFail($id);
            $shipment_obj->update(array("status"=>0));
            return redirect()->route('admin.shipment.index');
         }catch (ModelNotFoundException $e){
            return redirect()->route('nofound');
        }
    }
}
